package cn.edu.xmut.chen.zhlg.zhlg.message.service;

import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class WebSocketSessionService {
    private final Map<String, String> userSessionMap = new ConcurrentHashMap<>();
    private final SimpMessageSendingOperations messagingTemplate;

    public WebSocketSessionService(SimpMessageSendingOperations messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }

    public void registerSession(String userId, String sessionId) {
        userSessionMap.put(userId, sessionId);
        System.out.println("【会话管理】用户连接注册: 用户ID=" + userId + ", 会话ID=" + sessionId);
    }

    public void removeSession(String sessionId) {
        userSessionMap.entrySet().removeIf(entry -> entry.getValue().equals(sessionId));
        System.out.println("【会话管理】会话移除: 会话ID=" + sessionId);
    }

    public boolean isUserConnected(String userId) {
        return userSessionMap.containsKey(userId);
    }

    public void notifyUserStatus(String userId, boolean isOnline) {
        String status = isOnline ? "上线" : "下线";
        System.out.println("【用户状态】用户" + status + ": " + userId);

    }
}